var prefix = jsctx + "/orderInfo";

var saasTotalData = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {

        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listSaasTotalData",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 10,
                    pageList: [10, 20, 30],
                    // pageList: [10],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    showColumns: true,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            equipmentHlhtOperatorId: $("#equipmentHlhtOperatorId").val(),
                            hlhtOperatorId: $("#hlhtOperatorId").val(),
                            stationName: $("#stationName").val(),
                            startTime: $("#startTime").val(),
                            endTime: $("#endTime").val()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'stationName', title: '桩群', width: '120', align: 'left'},
                        {field: 'companyName', title: '所属公司', width: '120', align: 'left'},
                        {field: 'tenantName', title: '设备运营商', width: '120', align: 'left'},
                        {field: 'addressDetail', title: '桩群地址', width: '150', align: 'left'},
                        {field: 'hlhtOperatorName', title: '互联运营商', width: '150', align: 'left'},
                        {field: 'addTime', title: '桩群开通时间', width: '100', align: 'left'},
                        {field: 'useTime', title: '充电时间<br>(分钟)', width: '100', align: 'right'},
                        {field: 'totalUsePower', title: '充电量<br>(度)', width: '100', align: 'right'},
                        {field: 'totalChaAmount', title: '基本电费<br>(元)', width: '100', align: 'right'},
                        {field: 'totalSerAmount', title: '服务费<br>(元)', width: '100', align: 'right'},
                        {field: 'totalMoney', title: '总金额<br>(元)', width: '100', align: 'right'},
                        {field: 'totalDisAmount', title: '第三方优惠<br>(元)', width: '100', align: 'right'},
                        {field: 'totalPayAmount', title: '第三方实收金额<br>(元)', width: '100', align: 'right'},
                        {field: 'totalSaasDisAmount', title: 'saas优惠<br>(元)', width: '100', align: 'right'},
                        {field: 'totalSaasPayAmount', title: 'saas实收金额<br>(元)', width: '100', align: 'right'},
                        {field: 'useRatio', title: '使用率', width: '100', align: 'right'},
                    ]
                });
    }

    function reLoad() {
        var startTime = $("#startTime").val().trim();
        if (startTime == null || startTime == '') {
            layer.msg("请选择开始时间");
            return
        }
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    function printTotalData() {
        var startTime = $("#startTime").val().trim();
        if (startTime == null || startTime == '') {
            layer.msg("请选择开始时间");
            return
        }

        $.fileDownload(prefix + "/exportSaasTotalData", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                equipmentHlhtOperatorId: $("#equipmentHlhtOperatorId").val(),
                hlhtOperatorId: $("#hlhtOperatorId").val(),
                stationName: $("#stationName").val(),
                startTime: $("#startTime").val(),
                endTime: $("#endTime").val(),
                count: $("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        printTotalData: printTotalData
    };
})();
